﻿using Furion.DependencyInjection;
using Microsoft.Extensions.Caching.Memory;
using MyProject.Entity;
using MyProject.Enum;
using MyProject.IService.SystemManage;
using MyProject.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyProject.Cache
{
    public class ApiAuthorizeCache: ITransient
    {
        private readonly string _cacheKey = CacheKeys.ApiAuthorizeCache.ParseToString();

        private readonly IMemoryCache _memoryCache;
        private readonly IApiAuthorizeService _apiAuthorizeService;

        public ApiAuthorizeCache(IMemoryCache memoryCache, IApiAuthorizeService apiAuthorizeService)
        {
            _memoryCache = memoryCache;
            _apiAuthorizeService = apiAuthorizeService;
        }

        /// <summary>
        /// 根据url获取权限标识
        /// </summary>
        public async Task<List<ApiAuthorizeEntity>> GetAuthorizeByUrl(string url = "")
        {
            var list = _memoryCache.Get<List<ApiAuthorizeEntity>>(_cacheKey);
            if(list == null || list.Count() == 0)
            {
                list = await _apiAuthorizeService.GetList(null);
                _memoryCache.Set(_cacheKey, list) ;
            }

            if (url.IsEmpty())
                return list;

            return list.Where(a => a.Url.ToLower() == url.ToLower()).ToList();
        }

    }
}